package com.liner.collection;

import java.util.ArrayList;
import java.util.Collection;

/**
 * Collection接口
 * 所有集合的顶级接口，封装了所有集合所共有行为
 */
public class CollectionDemo {
    public static void main(String[] args) {
        Collection<Object> c = new ArrayList<>();

        c.add(new Point(1,2));
        c.add(new Point(3,4));
        c.add(new Point(4,6));
        c.add(new Point(8,9));
        c.add(new Point(1,2));


        c.add("one");
        c.add("two");
        c.add("three");
        c.add("one");
        c.add("two");
        c.add(1); //自动装箱为 Integer  基本数据类型和Object不沾边
        System.out.println(c);  //集合重写了Object的toString
        //[元素1.toString(),元素2.toString(),元素3.toString()]

        //contains
        Point point = new Point(1, 2);
        //比较两个对象是否值相等
        System.out.println("是否包含point:" + c.contains(point));  //包含point

        System.out.println("是否成功删除point:" + c.remove(point));    //删除point
        //若存在重复元素也只是删除一次
        System.out.println(c);


        System.out.println("size: " + c.size());
        //仅当size为0是为空集
        System.out.println("是否为空集:" + c.isEmpty());
        c.clear();  //清空集合

        System.out.println("size: " + c.size());
        //仅当size为0是为空集
        System.out.println("是否为空集:" + c.isEmpty());

        //集合里存放的是元素的引用
        ArrayList<Object> cc = new ArrayList<>();
        Point pp = new Point(1, 2);
        cc.add(pp); // 将pp添加到cc中   将 pp 元素的引用装到 cc 中
        System.out.println("pp:" + pp);
        System.out.println("cc:" + cc);

        pp.setX(100);
        System.out.println("pp:" + pp);
        System.out.println("cc:" + cc);



    }


}
